home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / File Mappi2910610152001.psc / frmMain.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-15  |  7.9 KB  |  195 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmMain 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Check PE Format"
  6.    ClientHeight    =   780
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   5850
  10.    Icon            =   "frmMain.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    LockControls    =   -1  'True
  13.    MaxButton       =   0   'False
  14.    MinButton       =   0   'False
  15.    ScaleHeight     =   780
  16.    ScaleWidth      =   5850
  17.    StartUpPosition =   2  'CenterScreen
  18.    Begin VB.CommandButton cmdViewPE 
  19.       Caption         =   "Check PE"
  20.       Height          =   315
  21.       Left            =   3240
  22.       TabIndex        =   3
  23.       Top             =   390
  24.       Width           =   1635
  25.    End
  26.    Begin MSComDlg.CommonDialog dlgLoadEXE 
  27.       Left            =   855
  28.       Top             =   90
  29.       _ExtentX        =   847
  30.       _ExtentY        =   847
  31.       _Version        =   393216
  32.       DefaultExt      =   "*.exe"
  33.       DialogTitle     =   "Select PE-Executable"
  34.       Filter          =   "Executable Files (*.exe)|*.exe|Dynamic Link Library (*.dll)|*.dll|All Files (*.*)|*.*"
  35.    End
  36.    Begin VB.CommandButton cmdBrowse 
  37.       Caption         =   "Browse..."
  38.       Height          =   285
  39.       Left            =   4890
  40.       TabIndex        =   1
  41.       Top             =   60
  42.       Width           =   855
  43.    End
  44.    Begin VB.TextBox PathFile 
  45.       Height          =   285
  46.       Left            =   750
  47.       TabIndex        =   0
  48.       Top             =   60
  49.       Width           =   4125
  50.    End
  51.    Begin VB.Label lblFName 
  52.       AutoSize        =   -1  'True
  53.       BackStyle       =   0  'Transparent
  54.       Caption         =   "Filename"
  55.       Height          =   195
  56.       Left            =   60
  57.       TabIndex        =   2
  58.       Top             =   90
  59.       Width           =   630
  60.    End
  61. Attribute VB_Name = "frmMain"
  62. Attribute VB_GlobalNameSpace = False
  63. Attribute VB_Creatable = False
  64. Attribute VB_PredeclaredId = True
  65. Attribute VB_Exposed = False
  66. ' This is a DEMO Application on Using my FileMapping VB Class Module
  67. '   to Simulate the use of File Mapping APIs
  68. '       Used in Visual C++ and Win32 Assembly, Why in Visual Basic, Why Not?
  69. '   This code checks for a valid PE Executable Format
  70. '   To understand this Application, you need to consult your nearest
  71. '   PE Documentation.
  72. '   Win32 Assembly Codes are included in Comments are 100% working on
  73. '   TASM32 Compiler
  74. '   Created by: Chris Vega [gwapo@models.com]
  75. '               http://trider.8m.com
  76. Private xPE As New clsFileMapping
  77. Private Sub cmdBrowse_Click()
  78.     dlgLoadEXE.ShowOpen
  79.     If Not Err Then PathFile = dlgLoadEXE.FileName
  80. End Sub
  81. Private Sub cmdViewPE_Click()
  82.     If xPE.OpenFile(Trim(PathFile)) Then
  83.         ' Open the File with Read Access-Rights
  84.         If xPE.MapFile Then
  85.             ' Create File Mapping Object with Page-Read Access
  86.             '   and Create a View of the Mapping Object
  87.             '       all with Read-Only Access
  88.             If xPE.OpenView Then
  89.                 '
  90.                 ' xPE Points to File Image Base
  91.                 ' ========================================================================
  92.                 '
  93.                 '       ; After File Mapping, set esi = ImageBase
  94.                 '
  95.                 '       ;   Note: ImageBase is the EntryPoint of a
  96.                 '       ;         Mapped File On View
  97.                 '
  98.                 '       xchg    eax, esi
  99.                 '       push    esi
  100.                 '       lodsw
  101.                 '       sub     ax,5a4dh
  102.                 '       jnz     @not_EXE
  103.                 '       pop     esi
  104.                 '
  105.                 With xPE
  106.                     ' Load Word from the Image Base
  107.                     MZ_Header = .lodsw
  108.                     If Hex(MZ_Header) <> "5A4D" Then  ' ZM Marker Found?
  109.                         '
  110.                         ' @not_EXE:
  111.                         '
  112.                         MsgBox "Not an EXE File!", vbExclamation, "Chris ROQ!"
  113.                         '
  114.                         '   jmp     @close_File
  115.                     Else
  116.                         ' Go to PE Header RVA Address (ImageBase + 3Ch)
  117.                         '
  118.                         '   ; esi still holds the Image Base
  119.                         '
  120.                         '   push    esi                     ; Save Image Base
  121.                         '   add     esi, 3ch                ; Point to lfaNew
  122.                         '   lodsd                           ; Get the PE Header RVA
  123.                         '   pop     esi                     ; Restore the Image Base
  124.                         '   add     eax,esi                 ; Align PE Header RVA
  125.                         '
  126.                         '   ; eax now Holds the PE Header Virtual Address
  127.                         '
  128.                         .SetFilePointer &H3C, SetIncreaseFromCurrent
  129.                         PE_Header = .lodsd
  130.                         .SetFilePointer .GetFileEntryPoint + PE_Header, _
  131.                                         SetReplaceCurrent
  132.                         '
  133.                         '   push    esi
  134.                         '   xchg    eax,esi
  135.                         '   lodsd
  136.                         '   sub     ax,4550h
  137.                         '   jnz     @not_PE
  138.                         '   pop     esi
  139.                         '
  140.                         PE_Header = .lodsd
  141.                         
  142.                         If Hex(PE_Header) = "4550" Then   ' EP Marker Found?
  143.                             '
  144.                             ' ;  We got a Valid PE File,
  145.                             ' ;    Lets go Check what type of PE is this!
  146.                             '
  147.                             '   mov     ax, word ptr [esi+18h]
  148.                             '   sub     ax, 010bh               ; PE32?
  149.                             '   jz      @found_PE32
  150.                             '   jmp     @found_PE64
  151.                             '
  152.                             '   ; 010b = PE32
  153.                             '   ; 020b = PE32+/PE64
  154.                             '
  155.                             .SetFilePointer &H18, SetIncreaseFromCurrent
  156.                             OH_Header = .lodsd
  157.                             
  158.                             OH_Magic = Right(Hex(OH_Header), 4)
  159.                             
  160.                             If OH_Magic = "010B" Then _
  161.                                 MsgBox "This is a valid PE32 Executable File!", _
  162.                                         vbInformation, _
  163.                                         "Chris Vega [gwapo@models.com]" Else _
  164.                                 MsgBox "This is a valid PE32+/PE64 Executable File!", _
  165.                                         vbInformation, _
  166.                                         "Chris Vega [gwapo@models.com]"
  167.                         Else
  168.                             '
  169.                             ' @not_PE:
  170.                             MsgBox "Not a valid PE File!", _
  171.                                    vbExclamation, _
  172.                                    "Chris Vega [gwapo@models.com]"
  173.                         End If
  174.                     End If
  175.                     .CloseView True     ' Close the View
  176.                     .CloseMap           ' and the Mapping Object
  177.                     .CloseFile          '   finally, Close the File
  178.                 End With
  179.             End If
  180.         Else
  181.             MsgBox "Error Creating Mapping Object.", vbExclamation
  182.         End If
  183.         '
  184.         ' @close_File:
  185.         '
  186.         xPE.CloseFile
  187.     Else
  188.         MsgBox "Error Opening the File", vbExclamation
  189.     End If
  190. End Sub
  191. Private Sub Form_Unload(Cancel As Integer)
  192.     MsgBox "Copyright 2001 by Chris Vega, No Rights Reserved, Use Without Permission!", _
  193.            vbInformation, "Chris Vega [gwapo@models.com]"
  194. End Sub
  195.